package ${package.Controller};

import java.util.List;

import org.jretty.apibase.PagerData;
import org.jretty.apibase.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
<#if restControllerStyle>
import org.springframework.web.bind.annotation.RestController;
<#else>
import org.springframework.stereotype.Controller;
</#if>

import ${package.ServiceImpl}.${table.serviceImplName};
import ${package.Entity}.${entity};
<#if superControllerClassPackage??>
import ${superControllerClassPackage};
</#if>

/**
 * <p>${table.comment} 前端控制器</p>
 *
 * @author ${author}
 * @since ${date}
 */
<#if restControllerStyle>
@RestController
<#else>
@Controller
</#if>
@RequestMapping("<#if controllerBasePath??>/${controllerBasePath}</#if><#if package.ModuleName??>/${package.ModuleName}</#if>/<#if controllerMappingHyphenStyle??>${controllerMappingHyphen}<#else>${table.entityPath}</#if>")
<#if kotlin>
class ${table.controllerName}<#if superControllerClass??> : ${superControllerClass}()</#if>
<#else>
<#if superControllerClass??>
public class ${table.controllerName} extends ${superControllerClass} {
<#else>
public class ${table.controllerName} {
	
	@Autowired
	private ${table.serviceImplName} dms;

    /**
     * 按条件 分页查询
     */
    @GetMapping("/list")
    // @RequiresPermissions("<#if controllerBasePath??>${controllerBasePath}</#if><#if package.ModuleName??>:${package.ModuleName}</#if>:<#if controllerMappingHyphenStyle??>${controllerMappingHyphen}<#else>${table.entityPath}</#if>:query") // 设置操作权限
    public Result<?> listByPage(
            @RequestParam(required = false) Long key,
            @RequestParam("pageNum") Integer pageNum, 
            @RequestParam("pageSize") Integer pageSize) {

        ${entity} params = new ${entity}();
        params.setId(key);

        PagerData<${entity}> list = dms.selectByPage(pageNum, pageSize, params);
        return Result.success(list);
    }

    /**
     * 查询（根据主键ID）
     */
    @GetMapping("/get/{id}")
    // @RequiresPermissions("<#if controllerBasePath??>${controllerBasePath}</#if><#if package.ModuleName??>:${package.ModuleName}</#if>:<#if controllerMappingHyphenStyle??>${controllerMappingHyphen}<#else>${table.entityPath}</#if>:query") // 设置操作权限
    public Result<?> getById(@PathVariable("id") Long id) {
        ${entity} entity = dms.selectByPrimaryKey(id);
        return Result.success(entity);
    }

    /**
     * 新增
     */
    @PostMapping("/add")
    // @SysLog("新增${table.entityPath}") // 操作审计
    // @RequiresPermissions("<#if controllerBasePath??>${controllerBasePath}</#if><#if package.ModuleName??>:${package.ModuleName}</#if>:<#if controllerMappingHyphenStyle??>${controllerMappingHyphen}<#else>${table.entityPath}</#if>:add") // 设置操作权限
    public Result<?> add(@RequestBody ${entity} entity) {
        // ValidatorUtils.validateEntity(entity); // 参数校验

        dms.insert(entity);
        return Result.success();
    }

    /**
     * 修改（根据主键ID）
     */
    @PostMapping("/update")
    // @SysLog("修改${table.entityPath}") // 操作审计
    // @RequiresPermissions("<#if controllerBasePath??>${controllerBasePath}</#if><#if package.ModuleName??>:${package.ModuleName}</#if>:<#if controllerMappingHyphenStyle??>${controllerMappingHyphen}<#else>${table.entityPath}</#if>:update") // 设置操作权限
    public Result<?> update(@RequestBody ${entity} entity) {
        // ValidatorUtils.validateEntity(entity); // 参数校验

        dms.update(entity);
        return Result.success();
    }

    /**
     * 删除（根据主键ID）
     */
    @RequestMapping(path = "/delete/{id}", method = { RequestMethod.GET, RequestMethod.POST })
    // @SysLog("删除${table.entityPath}") // 操作审计
    // @RequiresPermissions("<#if controllerBasePath??>${controllerBasePath}</#if><#if package.ModuleName??>:${package.ModuleName}</#if>:<#if controllerMappingHyphenStyle??>${controllerMappingHyphen}<#else>${table.entityPath}</#if>:delete") // 设置操作权限
    public Result<?> delete(@PathVariable("id") Long id) {
        dms.deleteByPrimaryKey(id);
        return Result.success();
    }

}
</#if>
</#if>
